﻿package mortal.game.view.guildNew.skill
{
    import Message.DB.Tables.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.guildNew.data.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class GuildNewSkillCell extends GSprite
    {
        protected var _bg:GImageBitmap;
        protected var _attName:GBitmap;
        protected var _cur:BitmapNumberText;
        protected var _totalNum:BitmapNumberText;
        protected var _splitLine:GBitmap;
        protected var _upBtn:GLoadedButton;
        protected var _line1:ScaleBitmap;
        protected var _line2:ScaleBitmap;
        protected var _curLevel:GTextFiled;
        protected var _nextLevel:GTextFiled;
        protected var _needTxt:GTextFiled;
        protected var _skillItem:BaseItem;
        protected var _bg2:GImageBitmap;
        protected var _attName2:GTextFiled;
        protected var _needLevel:GTextFiled;
        private var _sp2:GSprite;
        private var _hasGen1:Boolean = false;
        private var _hasGen2:Boolean = false;
        private var _skillShow:SkillInfo;
        private var _nextSkillInfo:SkillInfo;
        private var _skillMax:SkillInfo;
        private var _ckTime:int;

        public function GuildNewSkillCell()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._attName = UIFactory.gBitmap(ImagesConst.GuildNSkillLabel0, 42, 10, this);
            this._upBtn = UIFactory.gLoadedButton(ImagesConst.GuildNSkillUpBtn_upSkin, 48, 119, 51, 23, this);
            this._upBtn.label = "";
            this._upBtn.configEventListener(MouseEvent.CLICK, this.onBtnUpClickHandler);
            return;
        }// end function

        private function genSp1() : void
        {
            if (this._hasGen1)
            {
                return;
            }
            this._hasGen1 = true;
            this._bg = UIFactory.gImageBitmap(ImagesConst.GuildNSkillBgUp, 0, 0, this);
            this._cur = UIFactory.bitmapNumberText(55, 34, "GuildNSkillLevelNum2.png", 13, 16, -2, this, 13, 1);
            this._cur.text = "20";
            this._totalNum = UIFactory.bitmapNumberText(89, 34, "GuildNSkillLevelNum.png", 13, 16, -2, this, 13, 1);
            this._totalNum.text = "20";
            this._splitLine = UIFactory.gBitmap(ImagesConst.GuildNSkillDisFlag, 67, 34, this);
            this._curLevel = UIFactory.gTextField("", 11, 146, 146, 20, this, GlobalStyle.textFormatHuang2);
            this._nextLevel = UIFactory.gTextField("", 11, 164, 146, 20, this, GlobalStyle.textFormatLv);
            this._needTxt = UIFactory.gTextField("", 9, 185, 130, 20, this, GlobalStyle.textFormatPutong.center());
            this._skillItem = UIFactory.getUICompoment(BaseItem);
            this._skillItem.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
            UIFactory.setObjAttri(this._skillItem, 37, 48, -1, -1, this);
            this._line1 = UIFactory.bg(-6, 184, 165, 1, this, ImagesConst.SplitLine);
            this._line2 = UIFactory.bg(-7, 145, 165, 1, this, ImagesConst.SplitLine);
            this.addChild(this._upBtn);
            this.addChild(this._attName);
            return;
        }// end function

        private function genSp2() : void
        {
            if (this._hasGen2)
            {
                return;
            }
            this._hasGen2 = true;
            this._sp2 = UIFactory.getUICompoment(GSprite, 0, 0, this);
            this._sp2.mouseEnabled = true;
            this._bg2 = UIFactory.gImageBitmap(ImagesConst.GuildNSkillBgDisabled, 0, 0, this._sp2);
            this._skillItem = UIFactory.getUICompoment(BaseItem);
            this._skillItem.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
            UIFactory.setObjAttri(this._skillItem, 37, 48, -1, -1, this);
            this._attName2 = UIFactory.gTextField("", 0, 154, 146, 20, this._sp2, GlobalStyle.textFormatPutong.setColor(16777215).center());
            this._needLevel = UIFactory.gTextField("", 0, 184, 146, 20, this, GlobalStyle.textFormatHong.center());
            this._line1 = UIFactory.bg(-6, 184, 165, 1, this._sp2, ImagesConst.SplitLine);
            this._line2 = UIFactory.bg(-7, 145, 165, 1, this._sp2, ImagesConst.SplitLine);
            this._sp2.addChild(this._upBtn);
            this._sp2.addChild(this._attName);
            this._sp2.filters = [FilterConst.colorFilter2];
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._hasGen1 = false;
            this._hasGen2 = false;
            this._bg = null;
            this._attName = null;
            this._cur = null;
            this._totalNum = null;
            this._splitLine = null;
            this._upBtn = null;
            this._line1 = null;
            this._line2 = null;
            this._curLevel = null;
            this._nextLevel = null;
            this._needTxt = null;
            this._skillItem = null;
            this._sp2 = null;
            this._bg2 = null;
            this._attName2 = null;
            this._needLevel = null;
            return;
        }// end function

        public function updateInfo(param1:Object, param2:int) : void
        {
            var _loc_3:* = String(param1);
            var _loc_4:* = GuildNewUtil.getCurSchSkillIdByAttName(_loc_3);
            this.updateName(_loc_3);
            if (_loc_4 == 0)
            {
                this.genSp2();
                this.updateSp2(_loc_3);
            }
            else
            {
                this.genSp1();
                this.updateItem(_loc_4);
            }
            return;
        }// end function

        private function updateName(param1:String) : void
        {
            var _loc_2:* = GuildNewUtil.attNameToResource[param1];
            this._attName.bitmapData = GlobalClass.getBitmapData(ImagesConst["GuildNSkillLabel" + _loc_2]);
            return;
        }// end function

        private function updateSp2(param1:String) : void
        {
            var _loc_2:* = GuildNewUtil.getFirstSchoolLevel(param1);
            var _loc_3:* = GuildSchoolConfig.instance.getInfoByLevel(_loc_2);
            var _loc_4:* = int(_loc_3[param1]);
            var _loc_5:* = SkillConfig.instance.getInfoById(_loc_4);
            var _loc_6:* = SkillConfig.instance.getSkillByLevel(_loc_5.series, 1);
            this._attName2.text = _loc_6.skillDescription;
            this._needLevel.text = Language.getStringByParam(71610, _loc_2);
            this._skillItem.source = _loc_5.skillIcon + ".jpg";
            this._skillItem.filters = [FilterConst.colorFilter2];
            this._skillItem.toolTipData = _loc_5.name.replace(/\d+""\d+/, "0");
            this._upBtn.enabled = false;
            return;
        }// end function

        private function updateItem(param1:int) : void
        {
            var _loc_2:* = Cache.instance.guild.selfGuildInfo;
            var _loc_3:Boolean = true;
            var _loc_4:Boolean = false;
            this._skillItem.filters = null;
            this._skillMax = new SkillInfo();
            this._skillMax.tSkill = SkillConfig.instance.getInfoById(param1);
            this._skillShow = new SkillInfo();
            this._skillShow.learned = true;
            var _loc_5:* = Cache.instance.skill.getSkillBySerialId(this._skillMax.tSkill.series);
            if (_loc_5 == null)
            {
                this._skillShow.tSkill = SkillConfig.instance.getFirstSkillBySerialId(this._skillMax.tSkill.series);
                this._skillShow.learned = false;
                this._nextSkillInfo = this._skillShow;
            }
            else if (_loc_5.tSkill.skillLevel >= this._skillMax.tSkill.skillLevel)
            {
                this._nextSkillInfo = _loc_5;
                this._skillShow.tSkill = SkillConfig.instance.getSkillByLevel(_loc_5.tSkill.series, _loc_5.tSkill.skillLevel);
                this._upBtn.filters = null;
                _loc_3 = false;
                _loc_4 = true;
            }
            else
            {
                this._nextSkillInfo = new SkillInfo();
                this._skillShow.tSkill = SkillConfig.instance.getSkillByLevel(_loc_5.tSkill.series, _loc_5.tSkill.skillLevel);
                this._nextSkillInfo.tSkill = SkillConfig.instance.getSkillByLevel(_loc_5.tSkill.series, (_loc_5.tSkill.skillLevel + 1));
            }
            this._skillItem.source = this._skillShow.tSkill.skillIcon + ".jpg";
            if (_loc_5)
            {
                this._cur.text = "" + _loc_5.skillLevel;
                this._skillItem.toolTipData = this._skillShow.tSkill.name;
            }
            else
            {
                this._cur.text = "0";
                this._skillItem.toolTipData = "0" + this._skillShow.tSkill.name.replace(/\d""\d/, "");
            }
            this._totalNum.text = "" + this._skillMax.skillLevel.toString();
            this.updateAttri(_loc_4);
            this.updateContri(_loc_2, _loc_4, _loc_3);
            return;
        }// end function

        private function updateAttri(param1:Boolean) : void
        {
            if (this._skillShow.learned)
            {
                this._curLevel.text = Language.getString(71611) + this._skillShow.tSkill.skillDescription;
            }
            else
            {
                this._curLevel.text = Language.getString(71612);
            }
            if (param1)
            {
                this._nextLevel.text = "" + Language.getString(71613);
            }
            else
            {
                this._nextLevel.text = Language.getString(71614) + this._nextSkillInfo.tSkill.skillDescription;
            }
            return;
        }// end function

        private function updateContri(param1:SelfGuildInfo, param2:Boolean, param3:Boolean) : void
        {
            var _loc_4:Boolean = false;
            var _loc_5:String = null;
            if (param2)
            {
                this._needTxt.htmlText = "";
                this.enbaleUp(false);
            }
            else
            {
                _loc_4 = true;
                _loc_5 = "" + this._nextSkillInfo.tSkill.needGuildContribute;
                if (this._nextSkillInfo.tSkill.needGuildContribute > param1.selfInfo.contribution)
                {
                    this._upBtn.filters = null;
                    _loc_5 = HTMLUtil.addColor(_loc_5, GlobalStyle.colorHong);
                    _loc_4 = false;
                    this.enbaleUp(false);
                }
                else
                {
                    this.enbaleUp(true);
                }
                this._needTxt.htmlText = HTMLUtil.addColor(Language.getStringByParam(71615, _loc_5), GlobalStyle.colorPutong);
            }
            return;
        }// end function

        private function enbaleUp(param1:Boolean) : void
        {
            this._upBtn.enabled = param1;
            if (param1 && AutoGuideRedPoint3.instance.canSkillUpdate())
            {
                this._upBtn.filters = [FilterConst.itemChooseFilter];
            }
            else
            {
                this._upBtn.filters = null;
            }
            return;
        }// end function

        private function onBtnUpClickHandler(event:MouseEvent) : void
        {
            if (getTimer() - this._ckTime < 500)
            {
                return;
            }
            this._ckTime = getTimer();
            if (this._skillMax == null)
            {
                return;
            }
            if (GuildNewUtil.getSkillLeft() <= 0)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71616));
                return;
            }
            var _loc_2:* = Cache.instance.skill.getSkillBySerialId(this._skillMax.tSkill.series);
            if (_loc_2 && _loc_2.tSkill.skillLevel >= this._skillMax.tSkill.skillLevel)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71617));
                return;
            }
            var _loc_3:* = Cache.instance.guild.selfGuildInfo;
            if (this._nextSkillInfo.tSkill.needGuildContribute > _loc_3.selfInfo.contribution)
            {
                MsgManager.showMouseMsg("" + Language.getString(71618), MsgRollTipsType.msgRollTips3);
                return;
            }
            if (_loc_3.selfInfo.skillTimes >= GuildConst.GuildSkillLearnCount())
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71619));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_school_learn, this._nextSkillInfo.skillId));
            return;
        }// end function

    }
}
